const Service = require('egg').Service;

class MedicinalFoodService extends Service {

  // 新增药膳
  async create() {
    const { ctx } = this;
    const { name, description, ingredients, recommended_for } = ctx.request.body;
    return await ctx.app.mysql.insert('medicinal_foods', {
      name,
      description,
      ingredients,
      recommended_for
    });
  }

  // 删除药膳
  async delete() {
    const { ctx } = this;
    const id = ctx.params.id;
    return await ctx.app.mysql.delete('medicinal_foods', { id });
  }

  // 更新药膳
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const { name, description, ingredients, recommended_for } = ctx.request.body;
    return await ctx.app.mysql.update('medicinal_foods', {
      name,
      description,
      ingredients,
      recommended_for
    }, {
      where: { id }
    });
  }

  // 药膳列表
  async list() {
    const { ctx } = this;
    return await ctx.app.mysql.select('medicinal_foods');
  }
  
  async read() {
    const { ctx } = this;
    const id = ctx.params.id;
    return await ctx.app.mysql.get('medicinal_foods', { id });
  }
}

module.exports = MedicinalFoodService;
